package cn.iocoder.yudao.module.wine.dal.mysql.supplyinfo;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.ip.core.Area;
import cn.iocoder.yudao.framework.ip.core.enums.AreaTypeEnum;
import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.enums.SupplyInfoEnum;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppMarketReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyinfo.vo.AppSupplyInfoListReqVO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorder.enums.SupplyOrderEnum;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.membertype.MemberTypeDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyfavorite.SupplyFavoriteDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyinfo.SupplyInfoDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorder.SupplyOrderDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.userfans.UserFansDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.wine.controller.admin.supplyinfo.vo.*;

/**
 * 供求信息 Mapper
 *
 * @author 管理员
 */
@Mapper
public interface SupplyInfoMapper extends BaseMapperX<SupplyInfoDO> {

    default PageResult<SupplyInfoRespVO> selectPage(SupplyInfoPageReqVO reqVO) {
        return selectJoinPage(reqVO,SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .likeIfPresent(SupplyInfoDO::getInfoName, reqVO.getInfoName())
                .likeIfPresent(SupplyInfoDO::getInfoTitle, reqVO.getInfoTitle())
                .likeIfPresent(SupplyInfoDO::getDescription, reqVO.getDescription())

                .eqIfPresent(SupplyInfoDO::getUserId, reqVO.getUserId())
                .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                .eqIfPresent(SupplyInfoDO::getShowYear, reqVO.getShowYear())
                .eqIfPresent(SupplyInfoDO::getFragranceType, reqVO.getFragranceType())
                .eqIfPresent(SupplyInfoDO::getDegree, reqVO.getDegree())
                .eqIfPresent(SupplyInfoDO::getCapacity, reqVO.getCapacity())
                .eqIfPresent(SupplyInfoDO::getPacking, reqVO.getPacking())
                .eqIfPresent(SupplyInfoDO::getProductArea, reqVO.getProductArea())
                .eqIfPresent(SupplyInfoDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(SupplyInfoDO::getPackages, reqVO.getPackages())
                .eqIfPresent(SupplyInfoDO::getLabels, reqVO.getLabels())
                .eqIfPresent(SupplyInfoDO::getTags, reqVO.getTags())
                .eqIfPresent(SupplyInfoDO::getUnit, reqVO.getUnit())
                .eqIfPresent(SupplyInfoDO::getSalePrice, reqVO.getSalePrice())
                .eqIfPresent(SupplyInfoDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())

                .betweenIfPresent(SupplyInfoDO::getCreateTime, reqVO.getCreateTime())
                .leftJoin(MemberUserDO.class, MemberUserDO::getId,SupplyInfoDO::getUserId)
                .selectAll(SupplyInfoDO.class)
                .selectAs(MemberUserDO::getNickname,SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getName,SupplyInfoRespVO::getName)

                .orderByDesc(SupplyInfoDO::getUpdateTime)
                .orderByDesc(SupplyInfoDO::getId));
    }

    default PageResult<SupplyInfoRespVO> selectList(AppSupplyInfoListReqVO reqVO) {
        List<Integer> areaList = new ArrayList<>();
        if (reqVO.getAreaId() != null){
        Integer areaId = reqVO.getAreaId();
        Area area = AreaUtils.getArea(areaId);
            if(area.getType().equals(AreaTypeEnum.PROVINCE.getType())){
                List<Area> children = area.getChildren();
                List<Integer> collect = children.stream().map(item -> item.getId()).collect(Collectors.toList());
                collect.forEach(item -> {
                    Area area1 = AreaUtils.getArea(item);
                    List<Integer> collect1 = area1.getChildren().stream().map(data -> data.getId()).collect(Collectors.toList());
                    areaList.addAll(collect1);
                });
                areaList.addAll(collect);
            }
            if (area.getType().equals(AreaTypeEnum.CITY.getType())){
                List<Area> children = area.getChildren();
                List<Integer> collect = children.stream().map(item -> item.getId()).collect(Collectors.toList());
                areaList.addAll(collect);
            }
        }

        //region 关键词搜索

//        String keywords = reqVO.getKeywords();
//        if (!StringUtils.isEmpty(keywords)) {
//            String[] split = keywords.split(" ");
//            List<SupplyInfoRespVO> list = new ArrayList<>();
//            for (String s : split) {
//                List<SupplyInfoRespVO> supplyInfoRespVOS = selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
//                        .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
//                        .eqIfPresent(SupplyInfoDO::getStatus, SupplyInfoEnum.AUDIT.getCode())
//                        .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
//                        .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
//                        .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
//                        .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
//                        .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
//                        .inIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
//                        .likeIfExists(SupplyInfoDO::getInfoName, s)
//                        .in(!CollectionUtil.isEmpty(areaList),BusinessUserDO::getAreaId,areaList)
//                        .or(wrapper ->{
//                            wrapper.likeIfExists(SupplyInfoDO::getInfoTitle, s)
//                                 .in(!CollectionUtil.isEmpty(reqVO.getTypeIds()), SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
//                                .eqIfExists(SupplyInfoDO::getInfoType, reqVO.getInfoType())
//                                .eqIfExists(SupplyInfoDO::getStatus, SupplyInfoEnum.AUDIT.getCode())
//                                .eqIfExists(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
//                                .eqIfExists(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
//                                .eqIfExists(SupplyInfoDO::getIsTop, reqVO.getIsTop())
//                                .eqIfExists(SupplyInfoDO::getIsHot, reqVO.getIsHot())
//                                .eqIfExists(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
//                                    .in(!CollectionUtil.isEmpty(areaList),BusinessUserDO::getAreaId,areaList);})
//                        .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
//                        .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
//                        .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
//                        .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
//                        .eqIfExists(BusinessUserDO::getIsSelfSupport,reqVO.getIsSelfSupport())
//                        .selectAll(SupplyInfoDO.class)
//                        .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
//                        .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)
//                        .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
//                        .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
//                        .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)
//                        .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
//                        .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)
////                        .orderByDesc(SupplyInfoDO::getIsTop)
////                        .orderByDesc(SupplyInfoDO::getSort))
//                        .orderByDesc(SupplyInfoDO::getCreateTime));
//                list.addAll(supplyInfoRespVOS);
//            }
//            if (!CollectionUtil.isEmpty(list)) {
//                return list.stream().distinct().sorted(Comparator.comparing(SupplyInfoRespVO::getCreateTime).reversed()).collect(Collectors.toList());
//            }
//            return list;
//        }
        //endregion

        return selectJoinPage(reqVO, SupplyInfoRespVO.class,  new MPJLambdaWrapperX<SupplyInfoDO>()
                .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(SupplyInfoDO::getStatus, SupplyInfoEnum.AUDIT.getCode())
                .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                .inIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
                .likeIfExists(SupplyInfoDO::getInfoTitle, reqVO.getInfoTitle())
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .eqIfExists(BusinessUserDO::getIsSelfSupport,reqVO.getIsSelfSupport())

                .selectAll(SupplyInfoDO.class)

                .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)
                .in(!CollectionUtil.isEmpty(areaList),BusinessUserDO::getAreaId,areaList)
                .orderByDesc(SupplyInfoDO::getUpdateTime));
    }

    default List<SupplyInfoRespVO> getHomePageSupplyInfoList(AppSupplyInfoListReqVO reqVO) {
        return selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .eqIfPresent(SupplyInfoDO::getStatus, SupplyInfoEnum.AUDIT.getCode())
                .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                .inIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .eqIfExists(BusinessUserDO::getIsSelfSupport,2)
                .ne(SupplyInfoDO::getInfoType, SupplyInfoEnum.Cash_Out.getCode())//过滤套现

                .selectAll(SupplyInfoDO.class)

                .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)
                .orderByDesc(SupplyInfoDO::getUpdateTime)
                .last("limit 15"));//只查最新15条数据
    }

    default List<SupplyInfoRespVO> selectMySupplyInfoList(AppSupplyInfoListReqVO reqVO) {
        //关键词搜索
        String keywords = reqVO.getKeywords();
        if (!StringUtils.isEmpty(keywords)) {
            String[] split = keywords.split(" ");
            List<SupplyInfoRespVO> list = new ArrayList<>();
            for (String s : split) {
                List<SupplyInfoRespVO> supplyInfoRespVOS = selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                        .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                        .eqIfPresent(SupplyInfoDO::getStatus, SupplyInfoEnum.AUDIT.getCode())
                        .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                        .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                        .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                        .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                        .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                        .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                        .eqIfPresent(SupplyInfoDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                        .inIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
                        .likeIfExists(SupplyInfoDO::getInfoName, s)
                        .or(wrapper -> {
                            wrapper.likeIfExists(SupplyInfoDO::getInfoTitle, s)
                                    .eqIfExists(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                                    .eqIfExists(SupplyInfoDO::getStatus, SupplyInfoEnum.AUDIT.getCode())
                                    .eqIfExists(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                                    .eqIfExists(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                                    .eqIfExists(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                                    .eqIfExists(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                                    .eqIfExists(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                                    .eqIfExists(SupplyInfoDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                                    .in(!CollectionUtil.isEmpty(reqVO.getTypeIds()),SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
                            ;
                        })


                        .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                        .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                        .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                        .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)

                        .selectAll(SupplyInfoDO.class)

                        .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                        .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                        .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                        .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                        .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                        .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                        .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                        .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                        .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                        .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)

                        .orderByDesc(SupplyInfoDO::getUpdateTime)
                        .orderByDesc(SupplyInfoDO::getIsTop)
                        .orderByDesc(SupplyInfoDO::getSort));

                list.addAll(supplyInfoRespVOS);
            }
            if (!CollectionUtil.isEmpty(list)) {
                return list.stream().distinct().sorted(Comparator.comparing(SupplyInfoRespVO::getCreateTime).reversed()).collect(Collectors.toList());
            }
            return list;
        }
        return selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(SupplyInfoDO::getUserId, SecurityFrameworkUtils.getLoginUserId())
                .eqIfPresent(SupplyInfoDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                .likeIfPresent(SupplyInfoDO::getInfoName, reqVO.getInfoName())
                .inIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getTypeIds())
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyInfoDO.class)

                .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)

                .orderByDesc(SupplyInfoDO::getUpdateTime)
                .orderByDesc(SupplyInfoDO::getIsTop)
                .orderByDesc(SupplyInfoDO::getSort));
    }

    default SupplyInfoRespVO appGetSupplyInfoData(Long id) {
        return selectJoinOne(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .eqIfPresent(SupplyInfoDO::getId, id)
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .leftJoin(SupplyFavoriteDO.class, SupplyFavoriteDO::getSupplyInfoId, SupplyInfoDO::getId)
                .leftJoin(UserFansDO.class,
                        on -> on.eq(UserFansDO::getUserId, SupplyInfoDO::getUserId)
                                .eq(UserFansDO::getFansId, SecurityFrameworkUtils.getLoginUserId())
                )
                .selectAll(SupplyInfoDO.class)

                .selectAs(SupplyFavoriteDO::getId, SupplyInfoRespVO::getFavoriteId)

                .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)
                .selectAs(MemberUserDO::getMark, SupplyInfoRespVO::getMark)

                .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)
                .selectAs(UserFansDO::getSubscribeStatus, SupplyInfoRespVO::getSubscribeStatus)
        );
    }

    default List<SupplyInfoRespVO> getMarketSupplyInfoList(AppMarketReqVO reqVO){
        return selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .leIfPresent(SupplyInfoDO::getUpdateTime, reqVO.getCountTime())
                .eqIfExists(SupplyInfoDO::getCountName,reqVO.getCountName())
                .selectAll(SupplyInfoDO.class)
                .orderByDesc(SupplyInfoDO::getUpdateTime)
                .orderByDesc(SupplyInfoDO::getIsTop)
                .orderByDesc(SupplyInfoDO::getSort));
    }

    default List<SupplyInfoRespVO> getUserSupplyInfoList(AppSupplyInfoListReqVO reqVO){
        if (SupplyOrderEnum.COMPLETED.getCode().equals(reqVO.getCompleted())){
            return selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                    .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                    .eqIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                    .eqIfPresent(SupplyInfoDO::getUserId,reqVO.getUserId())
                    .eqIfPresent(SupplyInfoDO::getStatus, reqVO.getStatus())
                    .eqIfPresent(SupplyInfoDO::getTradeStatus, SupplyInfoEnum.SELL_OUT.getCode())
                    .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                    .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                    .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                    .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                    .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                    .likeIfPresent(SupplyInfoDO::getInfoName, reqVO.getInfoName())
                    .or(wrapper -> {
                        wrapper.eqIfExists(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                                .eqIfExists(SupplyInfoDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                                .eqIfExists(SupplyInfoDO::getUserId,reqVO.getUserId())
                                .likeIfExists(SupplyInfoDO::getInfoName, reqVO.getInfoName())
                                .eqIfExists(SupplyInfoDO::getStatus, reqVO.getStatus())
                                .eqIfExists(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                                .eqIfExists(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                                .eqIfExists(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                                .eqIfExists(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                                .eqIfExists(SupplyOrderDO::getStatus,SupplyOrderEnum.COMPLETED.getCode());
                    }
                    )
                    .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                    .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                    .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                    .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                    .selectAll(SupplyInfoDO.class)
                    .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                    .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                    .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                    .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                    .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                    .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                    .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                    .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                    .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                    .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)

                    .orderByDesc(SupplyInfoDO::getUpdateTime)
                    .orderByDesc(SupplyInfoDO::getIsTop)
                    .orderByDesc(SupplyInfoDO::getSort));
        }
        return selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                .eqIfPresent(SupplyInfoDO::getUserId,reqVO.getUserId())
                .eqIfPresent(SupplyInfoDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                .likeIfPresent(SupplyInfoDO::getInfoName, reqVO.getInfoName())
                .neIfPresent(SupplyInfoDO::getTradeStatus, SupplyInfoEnum.SELL_OUT.getCode())
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)

                .selectAll(SupplyInfoDO.class)

                .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)

                .orderByDesc(SupplyInfoDO::getUpdateTime)
                .orderByDesc(SupplyInfoDO::getIsTop)
                .orderByDesc(SupplyInfoDO::getSort));
    }


    default List<SupplyInfoRespVO> getUserSupplyShareList(AppSupplyInfoListReqVO reqVO){

        return selectJoinList(SupplyInfoRespVO.class, new MPJLambdaWrapperX<SupplyInfoDO>()
                .eqIfPresent(SupplyInfoDO::getInfoType, reqVO.getInfoType())
                .eqIfPresent(SupplyInfoDO::getSupplyTypeId, reqVO.getSupplyTypeId())
                .eqIfPresent(SupplyInfoDO::getUserId,reqVO.getUserId())
                .eqIfPresent(SupplyInfoDO::getStatus, reqVO.getStatus())
                .eqIfPresent(SupplyInfoDO::getTradeStatus, reqVO.getTradeStatus())
                .eqIfPresent(SupplyInfoDO::getIsDuty, reqVO.getIsDuty())
                .eqIfPresent(SupplyInfoDO::getIsTop, reqVO.getIsTop())
                .eqIfPresent(SupplyInfoDO::getIsHot, reqVO.getIsHot())
                .eqIfPresent(SupplyInfoDO::getIsRecommend, reqVO.getIsRecommend())
                .eqIfPresent(SupplyInfoDO::getIsConfig, reqVO.getIsConfig())
                .likeIfPresent(SupplyInfoDO::getInfoName, reqVO.getInfoName())
                .inIfPresent(SupplyInfoDO::getId, reqVO.getIds())
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyInfoDO::getUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyInfoDO.class)

                .selectAs(MemberUserDO::getNickname, SupplyInfoRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, SupplyInfoRespVO::getAvatar)

                .selectAs(BusinessCreditDO::getId, SupplyInfoRespVO::getCreditId)
                .selectAs(BusinessCreditDO::getName, SupplyInfoRespVO::getCreditName)
                .selectAs(BusinessCreditDO::getIcon, SupplyInfoRespVO::getIcon)

                .selectAs(BusinessUserDO::getIsSelfSupport, SupplyInfoRespVO::getIsSelfSupport)
                .selectAs(BusinessUserDO::getAreaId, SupplyInfoRespVO::getAreaId)
                .selectAs(BusinessUserDO::getBondName, SupplyInfoRespVO::getBondName)

                .selectAs(MemberTypeDO::getIcon, SupplyInfoRespVO::getMemberTypeIcon)
                .selectAs(MemberTypeDO::getName, SupplyInfoRespVO::getMemberTypeName)

                .orderByDesc(SupplyInfoDO::getUpdateTime)
                .orderByDesc(SupplyInfoDO::getIsTop)
                .orderByDesc(SupplyInfoDO::getSort));
    }
}
